package main;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import comunicacao.ReceiveMessageInterface;
import entities.Range;
import entities.ServerInfo;

/**
 * Utilidades disponiveis ao controlador primário
 * 
 * @author Leonardo Lino Nº45340
 * @author Diogo Pires Nº40351
 */
public class MasterUtils {
	
	public static void sendStartConditions(ServerInfo targetServer, ServerInfo provider, ServerInfo providerReplic, ServerInfo coordinator, Range mainRange, Range coordinatorRange, Range replicRange, int shardingLevel){
		Registry registry;
		ReceiveMessageInterface dbServer;
		String dbRegistryName = targetServer.getBindName();
		
        try {
			registry = LocateRegistry.getRegistry(targetServer.getAddress(), targetServer.getPort());
            dbServer = (ReceiveMessageInterface) (registry.lookup(dbRegistryName));
            if(shardingLevel == 0)
            	dbServer.loadDatabase(null, null, null, mainRange, null, null, shardingLevel);
            else if(shardingLevel == 1)
            	dbServer.loadDatabase(provider, null, null, mainRange, coordinatorRange, replicRange, shardingLevel);
            else
            	dbServer.loadDatabase(provider, providerReplic, coordinator, mainRange, coordinatorRange, replicRange, shardingLevel);
        } catch (RemoteException e) {
        	System.out.println("Error: in MasterUtils.assingResponsibilities");
        	e.printStackTrace();
        } catch (NotBoundException e) {
            System.out.println("Error: in MasterUtils.assingResponsibilities");
        }
	}
	
	public static Range calculateMainRange (Range range, int responsible){
		int min = range.getMin();
		int max = range.getMax();
		int size = range.getSize();
		
		//if size is even
		if ( (size & 1) == 0 )
			return new Range(min + (size/2), max, responsible);
		else
			return new Range(min + (size/2), max, responsible);
	}
	
	public static Range calculateReplicRange (Range range, int responsible){
		int min = range.getMin();
		int max = range.getMax();
		int size = range.getSize();
		
		//if size is even
		if ( (size & 1) == 0 )
			return new Range(min, min + (size/2) - 1 , responsible);
		else
			return new Range(min, min + (size/2) - 1, responsible);
	}
}
